package com.pdstudio.weather;

import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.WeatherSun;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SunActivity extends SwipeBackActivity implements EventHandler {

	private TextView txtTitle;
	private View customSunTitleView;
	private ImageView ivBack;
	private TextView tvRise;
	private TextView tvFall;
	private TextView tvTime;
	private Handler handler = null;
	
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sun);

		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_sun);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));


		tvRise = (TextView) findViewById(R.id.sun_rise_time);
		tvFall = (TextView) findViewById(R.id.sun_fall_time);
		tvTime = (TextView) findViewById(R.id.sun_time);

		customSunTitleView = findViewById(R.id.title_sun);
		txtTitle = (TextView) customSunTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("日出日落");

		ivBack = (ImageView) findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SunActivity.this.finish();

			}
		});

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					WeatherHttpMethod http = new WeatherHttpMethod(
							SunActivity.this);
					WeatherSun detail = new WeatherSun();
					detail = http.GetSun(mSpUtil.getAuthCode(),mApplication.getCurSimpleCity().cityCode);
					Message msg = handler.obtainMessage(0, detail);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {
					if (msg.what == 0) {

						// msg.obj是获取handler发送信息传来的数据
						WeatherSun detail = new WeatherSun();
						// Toast.makeText(getApplicationContext(), "数据加载中", 0)
						// .show();
						if (msg != null && msg.obj != null) {
							detail = (WeatherSun) msg.obj;
						}
						// 给ListView绑定数据
						setSun(detail);
					}
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setSun(WeatherSun detail) {
		if (detail != null) {
			tvRise.setText(detail.rise);
			tvFall.setText(detail.fall);
			tvTime.setText(mApplication.getCurSimpleCity().cityName);
		} else {
			tvTime.setText("目前没有日出日落信息");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sun, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub
		
	}
}
